package com.quanquan.management.service;

import com.quanquan.management.dao.AbstractBatchDao;
import com.quanquan.management.entity.SysPermission;
import com.quanquan.management.entity.SysPermissionRole;
import com.quanquan.management.repository.SysPermissionRepository;
import com.quanquan.management.repository.SysPermissionRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述:
 *
 * @ClassName: SysPermissionRoleService
 * @Author: xiaoqianh丶
 * @Date: 2019-08-13 15:33
 * @Version: V1.0
 */
@Service
public class SysPermissionRoleService extends AbstractBatchDao {

    @Autowired
    private SysPermissionRoleRepository sysPermissionRoleRepository;

    @Autowired
    private SysPermissionRepository sysPermissionRepository;

    /**
     * 角色添加权限
     *
     * @param roleId
     * @param permissionIds
     */
    public void addPermissionRole(String roleId, List<String> permissionIds) {
        //先删除所有该角色的权限再重新批量添加
        sysPermissionRoleRepository.removeShip(roleId);

        for (String permissionId : permissionIds) {
            SysPermissionRole sysPermissionRole = new SysPermissionRole();
            sysPermissionRole.setRoleId(roleId);
            sysPermissionRole.setPermissionId(permissionId);
            sysPermissionRoleRepository.saveAndFlush(sysPermissionRole);
        }


    }


    public List<String> queryPermissionIds(String roleId) {
        List<SysPermissionRole> sysPermissionRoleList = sysPermissionRoleRepository.findByRoleId(roleId);
        List<String> permissionIds = sysPermissionRoleList.stream().map(SysPermissionRole::getPermissionId).collect(Collectors.toList());
        return permissionIds;
    }



    public List<SysPermission> findAllPermission(){
        List<SysPermission> all = sysPermissionRepository.findAll();
        return all;
    }
}
